#ifndef SERFUNC_H
#define SERFUNC_H
#include <func.h>

typedef struct node_s{
    char pname[25]; //路径名称
    struct node_s* pnext;
    struct node_s* ppre;
}node_t;

typedef struct stack_s{
    node_t* top;
    node_t* buttom;
    int size;
}stack;

stack* initStack(stack* st){
    st = (stack*)malloc(sizeof(stack));
    if(st == NULL){
        return NULL;
    }
    st->size = 0;
    st->buttom = NULL;
    st->top = NULL;
    return st;
}
int push(stack* st, char* pname){
    node_t* newNode = (node_t*)calloc(1,sizeof(node_t));
    if(!newNode){
        ERROR_CHECK(newNode,NULL,"push-malloc");
        return -1;
    }
    //strncpy(newNode->pname,pname,strlen(pname));
    sprintf(newNode->pname,"%s",pname);
    //printf("%s\n",pname);
    newNode->pnext = NULL;
    newNode->ppre = NULL;
    if(st->size == 0)
    {
        st->top = newNode;
        st->buttom = st->top;
    }
    else
    {
        
        newNode->ppre = st->top;
        st->top->pnext = newNode;
        st->top = newNode;
        
    }
    st->size++;
    return 0;
}

int pop(stack* st, char* fname){
    if(st->size == 0){
        ERROR_CHECK(st->size,0,"stack is empty!");
        return -1;
    }
    //strncpy(fname,st->top->pname,strlen(st->top->pname));
    node_t* tmp = st->top;
    if(st->size == 1){
        st->top = st->buttom = NULL;
    }else{
        st->top = st->top->ppre;
        st->top->pnext = NULL; 
    }
    st->size--;
    free(tmp);
    return 0;
}

int peek(stack* st, char* fname){
    if(st->size == 0){
        ERROR_CHECK(st->size,0,"stack is empty!");
        fname = NULL;
        return -1;
    }
    strncpy(fname,st->top->pname,strlen(st->top->pname));
    return 0;
}

int printStack(stack* st){
    if(st == NULL){
        ERROR_CHECK(st,NULL,"printStack-> st is NULL");
        return -1;
    }
    node_t* head = st->buttom;
    if(head != NULL){
        printf("%s  ",head->pname);
        head = head->pnext;
    }
    printf("\n");
    return 0;
}

int destoryStack(stack* st){
    node_t* tail = st->top;
    while(tail != NULL){
        pop(st,NULL);
        //printf("pop!\n");
        tail = tail->ppre;
    }
}

int reservePrintf(stack* st){
    node_t* tail = st->top;
    while(tail){
        printf("%s ",tail->pname);
        tail = tail->ppre;
    }
    printf("\n");
    return 0;
}

//返回值-1代表cd失败，返回0表示cd成功，并将路径存入到curworkdir中
int myCd(stack* st, char* path,char* curWorkDir){
    printf("path:%s\n",path);
    //printf("curWorkingDir:%s\n",curWorkDir);
    char* cur = strtok(curWorkDir,"/");
    while(cur != NULL){
        push(st,cur);
        //printf("substr_cur: %s\n",cur);
        cur = strtok(NULL,"/");
    }
    char* p = strtok(path,"/");
    if(p == NULL){
        destoryStack(st);
        return 0;
    }
    while(p != NULL)
    {
        if(strcmp(p,"..") == 0){
            pop(st,NULL);
            //printf("leave\n");
        }
        else if(strcmp(p,".") == 0){
            //
        }else{
            push(st,p);
        }
        p = strtok(NULL,"/");
    }
    node_t* tmp = st->top;
    if(tmp==NULL)
    {
        //printf("路径不合法\n");
        return 0;
    }
    while(tmp->ppre != NULL){
        //printf("%s\n",tmp->pname);
        tmp = tmp->ppre;
    }
    char cdDir[128] = {0};
    sprintf(cdDir,"%s",tmp->pname);
    tmp = tmp->pnext;
    while(tmp != NULL){
        puts(cdDir);
        sprintf(cdDir,"%s/%s",cdDir,tmp->pname);
       
        tmp = tmp->pnext;
    }
    int isvalid = access(cdDir,F_OK);
    //printf("isvalid = %d\n",isvalid);
    //printf("before cdDir = %s\n",cdDir);
    destoryStack(st);
    if(isvalid == 0){
        sprintf(curWorkDir,"%s",cdDir);
        printf("cd success! curWorkDir: %s\n",cdDir);
        return 0;
    }else{
        //printf("curWorkingDir:%s\n",curWorkDir);
        printf("cd success!\n");
        return -1;
    }
}

#endif
